package com.cloud.poetry.system.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloud.poetry.base.entity.param.QueueParam;
import com.cloud.poetry.base.entity.res.SaveFileOutParam;
import com.cloud.poetry.base.feign.FileFeignClient;
import com.cloud.poetry.base.feign.QueueFeignClient;
import com.cloud.poetry.core.constant.QueueConstant;
import com.cloud.poetry.system.demo.entity.DemoUploadFile;
import com.cloud.poetry.system.manager.DemoManager;
import com.cloud.poetry.system.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mengxj
 */
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {

    @Resource
    private DemoManager demoManager;

    @Resource
    private FileFeignClient fileFeignClient;

    @Resource
    private QueueFeignClient queueFeignClient;

    @Override
    public List<String> tables(int type) {
        if(type == 1){
            return this.demoManager.tables1();
        }else if(type == 2){
            return this.demoManager.tables2();
        }else{
            throw new NullPointerException("param type is enabled");
        }
    }

    @Override
    public SaveFileOutParam uploadFile(DemoUploadFile file) {
        SaveFileOutParam saveRes = this.fileFeignClient.saveFile(file.getFile());
        log.info("上传成功，备注为：{} 返回结果为：{}", file.getRemark(), saveRes);
        return saveRes;
    }

    @Override
    public void push(int type, String queue) {
        QueueParam queueParam = new QueueParam();
        queueParam.setQueue(queue);
        queueParam.setQueueType(type == 1 ? QueueConstant.Type.KAFKA : QueueConstant.Type.RABBITMQ);
        Map<String, Object> param = new HashMap<>();
        param.put("type", type);
        param.put("queue", queue);
        queueParam.setJson(JSON.toJSONString(param));
        this.queueFeignClient.push(queueParam);
    }
}
